"""
@author: yjx
@file: 06.购物系统.py
@time: 2023/4/13 15:55
"""

'''
1.确定需求
2.几个类
系统类：
类属性：
实例属性：商品信息,系统说明信息，当前用户信息
方法：登陆注册，查看商品，购物车信息,结算，（充值，打印购买物品信息）

用户类：
类属性：
实例属性：余额,用户名，密码
方法：

'''

# 类命名   驼峰
# 函数命名   下划线

# 初始化用户类
class User:
    def __init__(self, name, passwd, initial_amount):
        self.name = name
        self.passwd = passwd
        self.initial_mount = initial_amount
        self.shopping_cart = {}




# 初始化系统信息
class Store:
    def __init__(self, goods_info, sys_info):
        self.goods_info = goods_info
        self.sys_info = sys_info
        self.user_dict = {}
    # 创建一个用户实例
    def register(self, name, passwd, initial_amount):
        if name not in self.user_dict:
            user = User(name, passwd, initial_amount)
            # 把name作为key，user对象作为value存在user.dict内
            self.user_dict[name] = user

            # print(user, type(user))
            # user的值是什么？
            # user_dict = {"name":"user"}
            # user_dict = {"name":"具体的名字？"}
        else:
            print("用户已存在！")
    # 查看购物车信息(前提要先登录成功，查看商品信息)
    def shoppingcart_info(self):
        self.shoppinfcart_dict = {}

fruit_goods = {
    "F01": {"name": "apple", "prise": 8},
    "F02": {"name": "banana", "prise": 4}
}
# 实例化一个水果购物系统
system1 = Store(fruit_goods, "水果购物系统")


medicine_goods = {
    "M01": {"name": "aos", "prise": 60},
    "M02": {"name": "virus", "prise": 30}
}
# 实例化一个药店购物系统
system2 = Store(medicine_goods, "药品购物系统")

system = [system1, system2]

while 1:
    print(f"当前平台已有购物系统如下：")
    # k下标， v具体值

    # v取到的是什么？ ”药品购物系统“  或者  ”水果购物系统“

    for k, v in enumerate(system):
        print(f"{k}.{v.sys_info}")
    c1 = input("请输入你的选择（按q退出系统）：")
    if c1 == "q":
        break
    if c1.isdigit() and int(c1) < len(system):
        c1 = int(c1)
        current_system = system[c1]
        print(f"欢迎进入{system[c1].sys_info}".center(40, "*"))
        while 1:
            c2 = input("请输入你的选择（1.登录2.注册,按q退出）：")
            if c2 == "q":
                break
            if c2 == "1":
                name, passwd = input("请输入你的名字、密码，用空格分割").split()
                if name in current_system.user_dict:
                    # print("用户名输入正确")
                    # print(current_system.user_dict[name].passwd)
                    if passwd == current_system.user_dict[name].passwd:
                        print("恭喜你登录成功")
                        c3 = input("1.查看商品信息2.查看购物车信息")
                        if c3 == "1":
                            print(f"我们现在有的商品是{current_system.goods_info}")
                            c4 = input("请输入你要加入购物车的商品编号：")
                            c4_name = set(current_system.goods_info)
                            # print(c4, type(c4))  # F01 <class 'str'>
                            # print(c4_name, type(c4_name)){'F02', 'F01'} <class 'set'>
                            c4_name = list(c4_name)
                            # print(c4_name, type(c4_name))# ['F02', 'F01'] <class 'list'>
                            # print(type(c4_name[0]))
                            if c4 in c4_name:
                                print("加入购物车成功！")
                                # print(current_system.goods_info[c4])
                                current_system.shoppinfcart_dict = {c4: current_system.goods_info[c4]}
                                print(f"你的购物车里有：{current_system.shoppinfcart_dict}")




            elif c2 == "2":
                name, passwd, amount = input("请输入你的名字、密码、初始金额，用空格分割").split()
                current_system.register(name, passwd, amount)
                # print(f"当前系统用户有{current_system.user_dict}")
                print(f"当前系统用户有{set(current_system.user_dict)}")


            else:
                print("输入有误！")
    else:
        print("输入有误，请重新输入")


